﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CF_DAL_WS.Models;
using CF_DAL_WS.Helpers;

namespace CF_DAL_WS.DTO
{
    public class OrdersDTO
    {
        public int ID { get; set; }
        public int IDRestaurant { get; set; }
        public string IDUsers { get; set; }
        public string OrderDate { get; set; }
        public string ShipDate { get; set; }
        public bool IsVerified { get; set; }
        public bool IsShipped { get; set; }
        public string DeliveryNote { get; set; }
        public string ReceiverAddress { get; set; }
        public string ReceiverName { get; set; }
        public string ReceiverPhone { get; set; }
        public UsersDTO User { get; set; }
        public RestaurantDTO Restaurant { get; set; }
        public List<OrderDetailDTO> OrderDetails { get; set; }

        public static OrdersDTO Translate(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    DeliveryNote = Entity.DeliveryNote,
                    ReceiverName = Entity.ReceiverName,
                    ReceiverAddress = Entity.ReceiverAddress,
                    ReceiverPhone = Entity.ReceiverPhone,
                    OrderDetails = new List<OrderDetailDTO>()
                };
                if (Entity.IDRestaurant != null)
                {
                    dto.IDRestaurant = Entity.IDRestaurant.Value;
                }
                if (Entity.Restaurant != null)
                {
                    dto.Restaurant = RestaurantDTO.TranslateBase(Entity.Restaurant);
                }
                if (Entity.IsVerified != null)
                {
                    dto.IsVerified = Entity.IsVerified.Value;
                }
                if (Entity.IsShipped != null)
                {
                    dto.IsShipped = Entity.IsShipped.Value;
                }
                if (Entity.IDUser != null)
                {
                    dto.IDUsers = Entity.IDUser.Value.ToString();
                }
                if (Entity.OrderDate != null)
                {
                    dto.OrderDate = string.Format("{0:MM/dd/yyyy HH:mm:ss}", Entity.OrderDate.Value);
                }
                if (Entity.ShipDate != null)
                {
                    dto.ShipDate = string.Format("{0:MM/dd/yyyy HH:mm:ss}", Entity.ShipDate.Value);
                }
                if (Entity.Users != null)
                {
                    dto.User = UsersDTO.TranslateBase(Entity.Users);
                    GlobalVariables.Logger.Info("Entity.Users: " + dto.User.ID);
                }
                if (Entity.OrderDetail != null)
                {
                    foreach (var item in Entity.OrderDetail)
                    {
                        dto.OrderDetails.Add(OrderDetailDTO.Translate(item));
                    }
                }
                return dto;
            }
            return null;
        }

        public static OrdersDTO TranslateBase(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    DeliveryNote = Entity.DeliveryNote,
                    ReceiverName = Entity.ReceiverName,
                    ReceiverAddress = Entity.ReceiverAddress,
                    ReceiverPhone = Entity.ReceiverPhone,
                };
                if (Entity.IDRestaurant != null)
                {
                    dto.IDRestaurant = Entity.IDRestaurant.Value;
                }
                if (Entity.IsVerified != null)
                {
                    dto.IsVerified = Entity.IsVerified.Value;
                }
                if (Entity.IsShipped != null)
                {
                    dto.IsShipped = Entity.IsShipped.Value;
                }
                if (Entity.IDUser != null)
                {
                    dto.IDUsers = Entity.IDUser.Value.ToString();
                }
                if (Entity.Users != null)
                {
                    dto.User = UsersDTO.TranslateBase(Entity.Users);
                }
                if (Entity.OrderDate != null)
                {
                    dto.OrderDate = string.Format("{0:MM/dd/yyyy HH:mm:ss}", Entity.OrderDate.Value);
                }
                if (Entity.ShipDate != null)
                {
                    dto.ShipDate = string.Format("{0:MM/dd/yyyy HH:mm:ss}", Entity.ShipDate.Value);
                }
            }
            return null;
        }

        public static Orders Reverse(OrdersDTO obj)
        {
            try
            {
                string[] supportedFormats = new String[] { "MM/dd/yyyy HH:mm:ss" };

                var entity = new Orders
                {
                    DeliveryNote = obj.DeliveryNote,
                    ReceiverName = obj.ReceiverName,
                    ReceiverAddress = obj.ReceiverAddress,
                    ReceiverPhone = obj.ReceiverPhone,
                    IDRestaurant = obj.IDRestaurant,
                    IDUser = Guid.Parse(obj.IDUsers),
                    IsVerified = obj.IsVerified,
                    IsShipped = obj.IsShipped,
                    OrderDate = DateTime.ParseExact(obj.OrderDate, supportedFormats,
                                                    System.Globalization.CultureInfo.CurrentCulture,
                                                    System.Globalization.DateTimeStyles.None)
                };
                if (obj.ShipDate != null && obj.ShipDate != string.Empty)
                {
                    entity.ShipDate = DateTime.ParseExact(obj.ShipDate, supportedFormats,
                                                    System.Globalization.CultureInfo.CurrentCulture,
                                                    System.Globalization.DateTimeStyles.None);
                }
                return entity;
            }
            catch (Exception ex)
            {
                GlobalVariables.Logger.Error(obj.IDUsers);
                GlobalVariables.Logger.Error(obj.OrderDate);
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public void UpdateEntity(ref Orders entity)
        {
            entity.IsVerified = IsVerified;
            entity.IsShipped = IsShipped;
            entity.ShipDate = DateTime.Parse(ShipDate);
        }
    }
}